<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2019/7/2
 * Time: 上午10:57
 */

class Custom extends Admin_Controller
{
    public function index()
    {
        $this->load->model('custom_purchase_model');

        $this->load->library('pagination');

        $assoc = $this->uri->uri_to_assoc();

        $step = intval($assoc['step']) ? intval($assoc['step']) : 1;

        $_param = array(
            'step' => intval($assoc['step']) ? intval($assoc['step']) : 1,
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );

        $param_option = array();
        switch ($step) {
            case 2:
                $param_option = array(
                    'custom_state' => array(
                        'title' => '状态',
                        'type' => 'checkbox',
                        'con' => 10,
                        'item' => array(
                            2 => '未采购',
                            3 => '待到货',
                            5 => '重新采购',
                            6 => '结束'
                        )
                    ),
                    'goods_sn' => array(
                        'title' => '产品货号',
                        'con' => 6,
                        'type' => 'text'
                    ),
                    'supp_id' => array(
                        'title' => '供应商',
                        'con' => 5,
                        'type' => 'search_select',
                        'url' => base_url('Custom/get_supp')
                    )
                );

                $_param['custom_state'] = $this->input->post('custom_state') ? explode(',', $this->input->post('custom_state')) : array(2,3,5);

                break;
        }

        $config['total_rows'] = $this->custom_purchase_model->custom_count($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->goods_model->limit_rows;
        $config['use_page_numbers']=true;
        $this->pagination->initialize($config);

        $custom = $this->custom_purchase_model->custom_list($_param);

        $data = array(
            'custom_list' => $custom['custom_list'],
            'pur_goods' => $custom['pur_goods'],
            'param' => $_param,
            'param_option' => $param_option,
            'pages' => $this->pagination->create_links(),
        );

        $this->load->view('custom_list_'.$_param['step'], $data);
    }

    public function add()
    {

        $this->load->model('goods_model');
        $this->load->model('goods_sku_model');
        $this->load->model('custom_purchase_model');

        $this->load->library('sys_message');
        $this->config->load('website_common');

        $assoc = $this->uri->uri_to_assoc();

        $step = $assoc['step'] ? intval($assoc['step']) : 1;

        $custom_id = intval($assoc['custom_id']);

        $custom = array();
        if ($custom_id) {
            $custom = $this->custom_purchase_model->info($custom_id);
        }


        switch ($step) {
            case 1:

                if ($_POST) {

                    $goods_sn = trim($this->input->post('goods_sn'));
                    !$goods_sn && $this->sys_message->warning('请填写货号！');

                    $goods = $this->goods_model->goods_info($goods_sn);

                    !$goods && $this->sys_message->warning('货号不存在！');

                    $sku = $this->goods_sku_model->sku_list(array('sku_ids' => $this->input->post('sku')));

                    $_sku = array();
                    foreach ($sku as $item) {
                        $_sku[$item['sku_id']] = array(
                            'sku_id' => $item['sku_id'],
                            'sku_sn' => $item['sku_sn'],
                            'color' => $item['color'],
                            'size' => $item['size']
                        );
                    }

                    $data = array(
                        'goods_id' => $goods['goods_id'],
                        'goods_sn' => $goods['goods_sn'],
                        'sku' => json_encode($_sku),
                        'custom_state' => 1
                    );

                    $custom_id = $this->custom_purchase_model->insert($data);

                    $this->sys_message->success('成功！');
                }

                break;
            case 2:

                $is_again = intval($assoc['is_again']);

                $this->load->model('suppliers_model');

                $custom['supp'] = $this->suppliers_model->suppliers_info(array(
                    'suppliers_id' => $custom['supp_id']
                ));

                if ($_POST) {

                    $data = array(
                        'supp_id' => intval($this->input->post('supp')),
                        'sample_delivery_time' => trim($this->input->post('delivery_time')),
                        'pattern_price' => floatval($this->input->post('pattern_price')),
                        'custom_state' => 2
                    );

                    !$data['supp_id'] && $this->sys_message->warning('请选择供应商！');
                    strtotime($data['sample_delivery_time']) < time() && $this->sys_message->warning('请填写正确的交货时间！');
                    !$data['pattern_price'] && $this->sys_message->warning('请填写打板费用！');


                    if ($is_again) {

                        $_data = array(
                            'goods_id' => $custom['goods_id'],
                            'goods_sn' => $custom['goods_sn'],
                            'sku' => $custom['sku'],
                            'pid' => $custom_id,
                            'custom_state' => 1
                        );

                        $custom_id = $this->custom_purchase_model->insert($_data);

                        $this->custom_purchase_model->update(array(
                            'custom_state' => 6,
                            'sample_remark' => '已重新打板，废弃'
                        ), $custom['custom_id']);

                    }

                    $this->custom_purchase_model->update($data, $custom_id);

                    $this->sys_message->success('成功！');
                }

                break;
            case 3:

                if ($_POST) {
                    $data = array(
                        'pattern_on' => trim($this->input->post('pattern_on')),
                        'custom_state' => 3
                    );

                    !$data['pattern_on'] && $this->sys_message->warning('请填写运单号！');

                    $this->custom_purchase_model->update($data, $custom_id);

                    $this->sys_message->success('成功！');
                }

                break;
            case 4:

                if ($_POST) {

                    $config = $this->config->item('upload');
                    $this->load->library('upload', $config);

                    $images = array();
                    foreach ($_FILES as $k => $item) {
                        if ($this->upload->do_upload($k)) {
                            $data_images = $this->upload->data();
                            $images[] = $data_images['file_name'];
                        }
                    }

                    $data = array(
                        'sample_img' => $images,
                        'custom_state' => intval($this->input->post('custom_state')),
                        'sample_remark' => trim($this->input->post('remark'))
                    );

                    !$data['sample_img'] && $this->sys_message->warning('请选择图片！');
                    !$data['custom_state'] && $this->sys_message->warning('请填写检测结果！');
                    !$data['sample_remark'] && $this->sys_message->warning('请填写原因！');

                    $this->custom_purchase_model->update($data, $custom_id);

                    $this->sys_message->success('成功！');
                }

                break;
            case 5:

                $custom['sku'] = json_decode($custom['sku'], true);

                if ($_POST) {

                    $this->load->library('eccang');

                    $this->load->model('suppliers_model');

                    $supp = $this->suppliers_model->suppliers_info(array(
                        'suppliers_id' => $custom['supp_id']
                    ));

                    $data = array(
                        'suppliers_id' => $supp['suppliers_name'],
                        'admin_id' => intval($this->input->post('admin_id')),
                        'date_eta' => trim($this->input->post('date_eta')),
                        'date_expected' => trim($this->input->post('date_expected'))
                    );
                    $sku_data = $this->input->post('sku');
                    $sku_price = $this->input->post('sku_price');

                    !$data['admin_id'] && $this->sys_message->warning('请选择采购员！');
                    !$data['date_expected'] && $this->sys_message->warning('请选择预计出厂时间！');
                    !$data['date_eta'] && $this->sys_message->warning('请选择预计到货时间！');
                    !$sku_data && $this->sys_message->warning('请填写采购数量！');
                    !$sku_price && $this->sys_message->warning('请填写采购单价！');

                    foreach ($sku_data as $k => $val) {
                        $data['productList'][] = array(
                            'product_sku' => $custom['sku'][$k]['sku_sn'],
                            'qty_expected' => $val,
                            'unit_price' => $sku_price[$k]
                        );
                    }

                    $purchase = $this->eccang->syncPurchaseOrders($data);

                    if ($purchase['code'] == 200) {

                        $this->custom_purchase_model->update(array(
                            'po_code' => $purchase['data']['poCode'],
                            'custom_state' => 8
                        ), $custom_id);
                    }

                    $this->sys_message->success('成功！');
                }

                break;
            case 6:

                $this->load->library('eccang');

                $PurchaseOrders = $this->eccang->getPurchaseOrders(array(
                    'code_like' => 'PO119071930165',
                    'pageSize' => 15
                ));

                print_r($PurchaseOrders);
                exit;

                if ($_POST) {

                }

                break;
        }

        $data = array(
            'custom' => $custom,
            'is_again' => $is_again
        );

        $this->load->view('custom_add_'.$step, $data);
    }

    public function get_supp()
    {

        $this->load->model('suppliers_model');

        $supp_name = trim($this->input->get('term'));

        $supp = $this->suppliers_model->suppliers_options(array('supp_name' => $supp_name), 0);

        $_results = array();
        foreach ($supp as $item) {
            $_results[] = array(
                'id' => $item['suppliers_id'],
                'text' => $item['suppliers_name']
            );
        }

        echo json_encode($_results);
        exit;
    }

    public function getsku()
    {
        $this->load->model('goods_sku_model');

        $this->load->library('sys_message');

        $assoc = $this->uri->uri_to_assoc();

        !$assoc['goods_sn'] && $this->sys_message->warning('请输入货号！');

        $sku_list = $this->goods_sku_model->sku_list(array('goods_sn' => $assoc['goods_sn']));
        !$sku_list && $this->sys_message->warning('货号不存在！');

        $this->sys_message->success('成功！', $sku_list);

    }
}
